package com.mikhailenko.transportMVC.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mikhailenko.transportMVC.exeption.ResultNotFoundExeption;
import com.mikhailenko.transportMVC.model.Result;
import com.mikhailenko.transportMVC.repository.ResultRepository;
import com.mikhailenko.transportMVC.service.ResultService;

@Service
public class ResultServiceImpl implements ResultService{

	@Autowired
	private ResultRepository resultRepository;
	
	@Override
	@Transactional
	public Result create(Result result) {
		Result createdResult = result; 
		return resultRepository.saveAndFlush(createdResult);
	}

	@Override
	@Transactional(rollbackFor=ResultNotFoundExeption.class)
	public Result delete(int id) throws ResultNotFoundExeption {
		Result deletedResult = resultRepository.findOne(id);
		
		if (deletedResult == null){
			throw new ResultNotFoundExeption();
		}
		
		resultRepository.delete(id);
		return deletedResult;
	}

	@Override
	@Transactional
	public List<Result> findAll() {
		return resultRepository.findAll();
	}

	@Override
	@Transactional
	public Result findById(int id) {
		return resultRepository.findOne(id);
	}

}
